define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",
        "text!templates/teacher/editapp.html",
        "models/ricevimentodocente",
        "views/common/menuview"
], function($, _, Backbone, Handlebars, template, Ricevimentodocente, MenuView) {
	
    var lectureschedule;
    var EditAppointmentView = Backbone.View.extend({

        template : Handlebars.compile(template),

        render: function(eventName) {
        	
            var obj = this.model.toJSON();
            if(this.model.get("orainizio")) {
                var ora_decimale = this.model.get("orainizio");
                var ora = parseInt(ora_decimale);
                ora_decimale = (ora_decimale - ora) * 60;
                var minuti = parseInt(ora_decimale);
                if(minuti == 0) minuti = "00";
                obj.orainizio = ora + ":" + minuti;
            }
            if(this.model.get("orafine")) {
                var ora_decimale = this.model.get("orafine");
                var ora = parseInt(ora_decimale);
                ora_decimale = (ora_decimale - ora) * 60;
                var minuti = parseInt(ora_decimale);
                if(minuti == 0) minuti = "00";
                obj.orafine = ora + ":" + minuti;
            }

        	this.$el.html(this.template(obj));

            // menu top
            this.menutop = new MenuView({
                el : $('#menu', this.el),
                model : this.model
            });
            this.menutop.render();

            return this;  
        },

        postRender: function(eventName) {
            $(".day").val(this.model.get("weekday"));
            $('.day').scroller({
                preset: 'select',
                theme: 'android-ics light',
                display: 'modal',
                mode: 'scroller'
            });

            $('.time.ora1').scroller({
                preset: 'time',
                theme: 'android-ics light',
                display: 'modal',
                mode: 'scroller',
                timeFormat: 'HH:ii',
                timeWheels: 'HHii',
                stepMinute: 15
            });

            $('.time.ora2').scroller({
                preset: 'time',
                theme: 'android-ics light',
                display: 'modal',
                mode: 'scroller',
                timeFormat: 'HH:ii',
                timeWheels: 'HHii',
                stepMinute: 15
            });
        },


        events: {
            'click #edit': 'updateappointment',
            'touchstart .button': 'over',
            'touchend .button': 'left',
            'touchcancel .button': 'left',
            'touchstart button': 'over',
            'touchend button': 'left',
            'touchcancel button': 'left',
            
        },


        updateappointment: function(event) {
            var ric = new Ricevimentodocente({
        		objectId: this.model.get('objectId'),
        	});
            ric.fetch();

            var orainizioArray = $('input.ora1').val().split(':');
            var orainizio = parseInt(orainizioArray[0]) + parseInt(orainizioArray[1])/60;

            var orafineArray = $('input.ora2').val().split(':');
            var orafine = parseInt(orafineArray[0]) + parseInt(orafineArray[1])/60;

            ric.save({
            	maxprenotazioni: parseInt($('input.maxp').val()),
        		orainizio: orainizio,
        	    orafine: orafine,
        	    stanza: $('input.stanza').val(),
        	    weekday: $('select.day').val()
            },{
            	success: function(){
            		alert('Appuntamento salvato');
                    // Backbone.history.back();
            	},
            	error: function(){
            		//alert('error updateappintment');
            	}
            	
            });
        },

      
        over: function(event) {
            $(event.currentTarget).addClass('fake-active');
        },

        left: function(event) {
            $(event.currentTarget).removeClass('fake-active');
        }
    });

    return EditAppointmentView;

});
